﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ERPStore.NewsLetters.EventConsumers
{
	public class CampaignStatTrackEventConsumer : EventBrokR.IConsumer<Events.NewsLetterTrackEvent>
	{
		public CampaignStatTrackEventConsumer(CloudSoft.Repositories.IRepository<Repositories.NewsLettersDbContext> repository)
		{
			this.Repository = repository;
		}

		protected CloudSoft.Repositories.IRepository<Repositories.NewsLettersDbContext>  Repository { get; private set; }

		public void Handle(Events.NewsLetterTrackEvent eventMessage)
		{
			var campaignStat = Repository.Get<Repositories.Datas.CampaignStatData>(i => i.CampaignStatId == eventMessage.Track.CampaignId);
			if (campaignStat == null)
			{
				campaignStat = new Repositories.Datas.CampaignStatData();
				campaignStat.CampaignStatId = eventMessage.Track.CampaignId;
			}

			campaignStat.ClickCount++;

			Repository.Update(campaignStat);
		}

	}
}
